package com.xiaobo.framework.endpoint;

import com.xiaobo.framework.common.exception.BizException;
import com.xiaobo.framework.common.kits.ObjectKits;
import com.xiaobo.framework.endpoint.fegin.OpenFegin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: <a href="get_xiaobo@163.com">Josh_Xu</a>
 * @description:
 * @create: 2020-05-24 12:33
 **/
@RestController
@RequestMapping("/mall-api")
@Api(tags = "商城api")
public class MallEndpoint {

    @Autowired
    private OpenFegin openFegin;

    private static final Logger logger = LoggerFactory.getLogger(MallEndpoint.class);

    @ApiOperation(value = "获取公钥", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, response = Object.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "服务器异常")
    })
    @GetMapping(value = "/v1/get-public-key", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<?> publicKey() {
        try {
            ResponseEntity<?> responseEntity = openFegin.publicKey();
            if (responseEntity.getStatusCode().equals(HttpStatus.OK)){
                Object body = responseEntity.getBody();
                String token = body.toString();
                token.substring(1,token.length()-1);
                HashMap<String, String> map = new HashMap<>();
                map.put(token.split("=")[0],token.split("=")[1]);
                return ResponseEntity.ok(map);
            }
            return ResponseEntity.ok().build();

        } catch (BizException ex) {
            logger.error("消息错误", ex);
        }
        return ResponseEntity.ok().build();
    }

}
